package com.example.managemyself.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 数据库设置管理器
 */
public class DatabaseSettingsManager {
    private static final String PREF_NAME = "database_settings";
    
    // 数据库配置相关的键
    private static final String KEY_USE_REMOTE_DATABASE = "use_remote_database";
    private static final String KEY_DB_HOST = "db_host";
    private static final String KEY_DB_PORT = "db_port";
    private static final String KEY_DB_NAME = "db_name";
    private static final String KEY_DB_USER = "db_user";
    private static final String KEY_DB_PASSWORD = "db_password";
    
    // 单例实例
    private static DatabaseSettingsManager instance;
    
    // 私有构造函数
    private DatabaseSettingsManager() {}
    
    /**
     * 获取单例实例
     */
    public static synchronized DatabaseSettingsManager getInstance() {
        if (instance == null) {
            instance = new DatabaseSettingsManager();
        }
        return instance;
    }
    
    /**
     * 是否使用远程数据库
     */
    public boolean isUseRemoteDatabase(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean(KEY_USE_REMOTE_DATABASE, false);
    }
    
    /**
     * 设置是否使用远程数据库
     */
    public void setUseRemoteDatabase(Context context, boolean useRemoteDatabase) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(KEY_USE_REMOTE_DATABASE, useRemoteDatabase).apply();
    }
    
    /**
     * 获取数据库主机地址
     */
    public String getDbHost(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_DB_HOST, "");
    }
    
    /**
     * 设置数据库主机地址
     */
    public void setDbHost(Context context, String host) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(KEY_DB_HOST, host).apply();
    }
    
    /**
     * 获取数据库端口
     */
    public String getDbPort(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_DB_PORT, "3306");
    }
    
    /**
     * 设置数据库端口
     */
    public void setDbPort(Context context, String port) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(KEY_DB_PORT, port).apply();
    }
    
    /**
     * 获取数据库名称
     */
    public String getDbName(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_DB_NAME, "");
    }
    
    /**
     * 设置数据库名称
     */
    public void setDbName(Context context, String name) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(KEY_DB_NAME, name).apply();
    }
    
    /**
     * 获取数据库用户名
     */
    public String getDbUser(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_DB_USER, "");
    }
    
    /**
     * 设置数据库用户名
     */
    public void setDbUser(Context context, String user) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(KEY_DB_USER, user).apply();
    }
    
    /**
     * 获取数据库密码
     */
    public String getDbPassword(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_DB_PASSWORD, "");
    }
    
    /**
     * 设置数据库密码
     */
    public void setDbPassword(Context context, String password) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(KEY_DB_PASSWORD, password).apply();
    }
    
    /**
     * 保存所有数据库设置
     */
    public void saveAllSettings(Context context, boolean useRemote, String host, String port, 
                               String name, String user, String password) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        
        editor.putBoolean(KEY_USE_REMOTE_DATABASE, useRemote);
        editor.putString(KEY_DB_HOST, host);
        editor.putString(KEY_DB_PORT, port);
        editor.putString(KEY_DB_NAME, name);
        editor.putString(KEY_DB_USER, user);
        editor.putString(KEY_DB_PASSWORD, password);
        
        editor.apply();
    }
} 